package cn.sdut.socket.bio.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * Created by majiatao
 */
public class UdpReceiver {
    public static void main(String[] args) throws Exception{
        // 1. 创建UDP的Socket服务监听接收数据的端口
        DatagramSocket socket = new DatagramSocket(9090);
        // 2. 创建接收数据的数据包
        byte[] dataByte = new byte[1024];
        DatagramPacket packet = new DatagramPacket(dataByte, 0, dataByte.length);
        // 3. 通过UDP的receive方法接收数据，并封装在数据包中
        socket.receive(packet);
        // 4. 解析数据包
        System.out.println(new String(packet.getData(), 0, packet.getData().length));
        // 5. 关闭Socket
        socket.close();
    }
}
